package org.rslai.tcedit.web;

import javax.servlet.http.*;
import org.rslai.tcedit.security.SecurityUtil;
import org.rslai.tcedit.util.SessionUtil;
import org.rslai.tcedit.web.core.*;

/**
 * 检查用户状态和显示产品和版本
 * 
 * @author rslai
 * 
 * @spring.bean name="/checkState.jspx"
 */
public class CheckStateController extends AbstractScriptController {

	private static final String NOT_LOGIN = "var login=false;\ndocument.write(\"[ <a href='javascript:void(0)' onClick='goLogin(this)'>登录</a> ]\");\n";

	public String getScript(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String username = SecurityUtil.getCurrentUsernameOrNull();
		if ((username != null) && !username.equals("roleAnonymous")) {
			StringBuffer strBuf = new StringBuffer();
			strBuf.append( "var login=true;\n" );
			strBuf.append( "document.write(\"" );
			strBuf.append( "欢迎 " + username + " ！[ <a href='' onClick='goLogout(this)'>注销</a> ]");
			String productName = SessionUtil.getCurrentProductName(request);
			if (productName.equals("")) {
				strBuf.append("<br>[ <a href='javascript:void(0)' onClick='goSelectProductVersion(this)'>选择产品及版本</a> ]");
			} else {
				strBuf.append( "<br>产品：" + SessionUtil.getCurrentProductName(request) );
				strBuf.append( " 版本：" + SessionUtil.getCurrentVersionName(request) );
				strBuf.append( " [ <a href='javascript:void(0)' onClick='goSelectProductVersion(this)'>切换</a> ]" );
			}
			strBuf.append("\");\n");

			return strBuf.toString();
		}
		return NOT_LOGIN;
	}

	public int getCacheTime(HttpServletRequest request,
			HttpServletResponse response) {
		return 0;
	}

}
